import Taro from '@tarojs/taro'
import {action, computed, observable} from 'mobx'
import $http from "../utils/http";

// 列表项
export interface ListItem {
  create_time: string;
  id: number;
  image: string;
  level: number;
  project_id: number;
  remark: string;
  status: number;
  user_name: string;
}

// 等级
export const LEVEL1 = '1'
export const LEVEL2 = '2'
export const LEVEL3 = '3'

export default class InputStore {
  @observable project_id: string = ''
  @observable name: string = ''
  @observable bearing_load: string = ''
  @observable construction_unit: string = ''
  @observable use_unit: string = ''
  @observable construction_time: string = ''
  @observable dismantle_time: string = ''
  @observable remark: string = ''
  @observable lists: ListItem[] = []
  @observable level: string = '1' // 级别
  @observable selectList: number[][] = [[], [], []] // checkbox列表
  // 级别对应数据
  levelMapStr: Object = {
    1: '一',
    2: '二',
    3: '三'
  }
  // 级别对应数据2
  levelMapInt: Object = {
    1: LEVEL1,
    2: LEVEL2,
    3: LEVEL3
  }

  @computed get btnOn() {
    const level = parseInt(this.level)
    for (let i = 0; i < this.selectList.length; i++) {
      const el = this.selectList[i];
      if (this.level === this.levelMapInt[i + 1]) {
        if (
          !this.bearing_load || // 未填写载重
          !this.lists[i] ||
          !this.lists[i].user_name || // 未填写姓名
          el.length === 0 || // 未选择是否合格
          !this.remark // 未填写备注
        ) return true
      }
    }
    return false
  }

  @action
  clear() {
    this.project_id = ''
    this.name = ''
    this.bearing_load = ''
    this.construction_unit = ''
    this.use_unit = ''
    this.construction_time = ''
    this.dismantle_time = ''
    this.remark = ''
    this.lists = []
    this.level = '1' // 级别
    this.selectList = [[], [], []] // checkbox列表
  }

  @action
  async getDataToScanUrl(url: String) {
    Taro.showLoading({ title: '加载中...', mask: true })
    const result = await $http
      .post(url)
      .catch(() => Taro.hideLoading())
    Taro.hideLoading()
    if (result) {
      const { code, rows } = result
      if (code === 100) {
        const nextLevel = rows.lists.length + 1
        this.project_id = rows.id
        this.name = rows.name
        this.bearing_load = rows.bearing_load
        this.construction_unit = rows.construction_unit
        this.use_unit = rows.use_unit
        this.construction_time = rows.construction_time
        this.dismantle_time = rows.dismantle_time
        this.lists = rows.lists
        this.level = nextLevel > 3 ? '3' : nextLevel + ''
        this.selectList.forEach((_, index) => {
          if (rows.lists[index]) {
            this.selectList[index] = [rows.lists[index].status]
          }
        })
        if (rows.lists.length >= 3) {
          Taro.showToast({title: '工程已完成3次验收', icon: 'none'})
          this.remark = rows.lists[2].remark
        }
      }
    }
  }

  @action
  async saveCheck(params) {
    // project_id //项目id
    // level //验收等级
    // user_name //验收用户
    // status //状态 1-合格 2-不合格
    // image //图片附件
    // remark //备注
    if (this.btnOn) return
    const index = parseInt(this.level) - 1
    Taro.showLoading({ title: '正在提交...', mask: true })
    const result = await $http
      .post('http://project.weitip.com/index/tool/add_check', {
        project_id: this.project_id,
        level: this.level,
        user_name: this.lists[index].user_name,
        status: this.selectList[index][0],
        remark: this.remark,
        ...params
      })
      .catch(() => Taro.hideLoading())
    Taro.hideLoading()
    if (result) {
      const { code, msg } = result
      if (code === 100) {
        Taro.showToast({title: '提交成功'})
        Taro.navigateBack()
      } else {
        Taro.showToast({title: msg, icon: 'none'})
      }
    }
  }
}
